<?php
namespace app\http;

use think\worker\Server;

class Worker extends Server
{
	protected $socket = 'websocket://127.0.0.1:2346';
    private $all = [];
 
    /**
    * 收到信息
    * @param $connection
    * @param $data
    */
    public function onMessage($connection, $data)
    {
        // 广播
        foreach ($this->all as $v) {
            $v->send($data);	
        }
        // $connection->send("收到消息啦，您的id:" . $connection->id);
    }
 
    /**
    * 当连接建立时触发的回调函数
    * @param $connection
    */
    public function onConnect($connection)
    { 
        // 保存所有已连接的客户端id
        $this->all[$connection->id] = $connection;
    }
    /**
    * 当连接断开时触发的回调函数
    * @param $connection
    */
    public function onClose($connection)
    { 
        // 去除断开连接的客户端
        unset($this->all[$connection->id]);
    }
 
    /**
    * 当客户端的连接上发生错误时触发
    * @param $connection
    * @param $code
    * @param $msg
    */
    public function onError($connection, $code, $msg)
    {
        echo "error $code $msg";
    } 
 
    /**
    * 每个进程启动
    * @param $worker
    */
    public function onWorkerStart($worker)
    {
 
    }
}